﻿using System.Drawing;
using System.Windows.Forms;
using Syncfusion.Drawing;
using Syncfusion.GridHelperClasses;
using Syncfusion.Windows.Forms;
using Syncfusion.Windows.Forms.Grid;
using Syncfusion.Windows.Forms.Grid.Grouping;
using Syncfusion.Windows.Forms.Tools;
using Syncfusion.GridExcelConverter;
using Syncfusion.GroupingGridExcelConverter;

namespace PMngr.Main
{
    public static class GridHelper
    {
        public static void InitializeGridGrouping(this GridGroupingControl groupCtl)
        {
            #region GridGroupingControl Settings
            // Column style
            //groupCtl.TableDescriptor.AllowCalculateMaxColumnWidth = true;
            groupCtl.TableOptions.ColumnsMaxLengthFirstNRecords = 10;
            groupCtl.TableOptions.ColumnsMaxLengthStrategy = GridColumnsMaxLengthStrategy.FirstNRecords;
            // Appearance
            //groupCtl.Appearance.AnyRecordFieldCell.AutoSize = true;
            groupCtl.Appearance.AnyCell.WrapText = false;
            groupCtl.Appearance.AnyCell.Trimming = StringTrimming.EllipsisCharacter;
            groupCtl.Appearance.AnyCell.HorizontalAlignment = GridHorizontalAlignment.Left;
            groupCtl.Appearance.AnyCell.TextMargins.Left = 3;
            groupCtl.Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.Static;
            groupCtl.Appearance.AlternateRecordFieldCell.Interior = new BrushInfo(Color.Gainsboro);

            groupCtl.TopLevelGroupOptions.ShowCaption = false;
            groupCtl.TopLevelGroupOptions.ShowAddNewRecordBeforeDetails = false;

            groupCtl.TableOptions.DefaultColumnWidth = 100;
            groupCtl.TableOptions.GridVisualStyles = GridVisualStyles.Office2007Silver;
            groupCtl.TableOptions.GridLineBorder = new GridBorder(GridBorderStyle.Solid, Color.Silver, GridBorderWeight.Thin);
            groupCtl.TableOptions.AllowSelection = GridSelectionFlags.None;
            groupCtl.TableOptions.SelectionTextColor = Color.WhiteSmoke;
            groupCtl.TableOptions.SelectionBackColor = SystemColors.Highlight;
            groupCtl.TableOptions.ListBoxSelectionMode = SelectionMode.One;
            groupCtl.TableOptions.ListBoxSelectionCurrentCellOptions = GridListBoxSelectionCurrentCellOptions.HideCurrentCell;
            groupCtl.TableModel.Options.ShowCurrentCellBorderBehavior = GridShowCurrentCellBorder.HideAlways;
            // ScrollBar
            groupCtl.Office2007ScrollBars = true;
            groupCtl.Office2007ScrollBarsColorScheme = Office2007ColorScheme.Silver;
            #endregion
        }

        public static void InitializeGridGroupingWithFormat(this GridGroupingControl groupCtl, string expression)
        {
            InitializeGridGroupingWithFormat(groupCtl, expression, Color.IndianRed, true);
        }

        public static void InitializeGridGroupingWithFormat(this GridGroupingControl groupCtl, string expression, Color color, bool strikeOut)
        {
            InitializeGridGrouping(groupCtl);
            // ConditionalFormat
            var conditionalFormat = SetConditionFormat(expression, color, strikeOut);
            groupCtl.TableDescriptor.ConditionalFormats.Add(conditionalFormat);
        }

        public static void InitializeGridGroupingWithFilter(this GridGroupingControl groupCtl)
        {
            InitializeGridGrouping(groupCtl);
            SetFilter(groupCtl);
        }

        public static void InitializeGridGroupingWithFilterAndFormat(this GridGroupingControl groupCtl, string expression)
        {
            InitializeGridGrouping(groupCtl);
            // ConditionalFormat
            var conditionalFormat = SetConditionFormat(expression, Color.IndianRed, true);
            groupCtl.TableDescriptor.ConditionalFormats.Add(conditionalFormat);
            // Filter
            SetFilter(groupCtl);
        }

        public static GridConditionalFormatDescriptor SetConditionFormat(string expression, Color color, bool strikeOut)
        {
            var conditionalFormat = new GridConditionalFormatDescriptor();
            conditionalFormat.Appearance.AnyRecordFieldCell.Interior = new BrushInfo(color);
            conditionalFormat.Appearance.AnyRecordFieldCell.Font.Strikeout = strikeOut;
            conditionalFormat.Expression = expression;
            return conditionalFormat;
        }

        public static void SetFilter(this GridGroupingControl groupCtl)
        {
            var filter = new GridDynamicFilter();
            // ShowFilterBar
            groupCtl.TopLevelGroupOptions.ShowFilterBar = true;
            groupCtl.TableDescriptor.Appearance.FilterBarCell.BackColor = Color.WhiteSmoke;
            foreach (GridColumnDescriptor t in groupCtl.TableDescriptor.Columns)
                t.AllowFilter = true;
            // Enable dynamic filter.
            filter.WireGrid(groupCtl);
        }

        public static void ExportToExcel(this GridGroupingControl groupCtl, string fileName)
        {
            var converter = new GroupingGridExcelConverterControl
            {
                ExportStyle = true,
                ExportBorders = true,
                HeaderBackColor = Color.FromArgb(10147522),
                CaptionBackColor = Color.Silver
            };

            converter.GroupingGridToExcel(groupCtl, fileName, ConverterOptions.Visible);
            converter.Dispose();
        }
    }

    public static class DateTimeHelper
    {
        public static void InitializeDateTimePicker(this DateTimePickerAdv dt)
        {
            dt.Calendar.NoneButton.Appearance = ButtonAppearance.Office2007;
            dt.Calendar.NoneButton.Location = new Point(146, 0);
            dt.Calendar.NoneButton.Office2007ColorScheme = Office2007Theme.Silver;
            dt.Calendar.NoneButton.Size = new Size(86, 21);
            dt.Calendar.NoneButton.Text = @"取消";
            dt.Calendar.NoneButton.UseVisualStyle = true;
            // 
            dt.Calendar.TodayButton.Appearance = ButtonAppearance.Office2007;
            dt.Calendar.TodayButton.Location = new Point(0, 0);
            dt.Calendar.TodayButton.Office2007ColorScheme = Office2007Theme.Silver;
            dt.Calendar.TodayButton.Size = new Size(146, 21);
            dt.Calendar.TodayButton.Text = @"今天";
            dt.Calendar.TodayButton.UseVisualStyle = true;
        }

    }
}
